package com.study.login.controller;

import com.study.common.entity.LoginException;
import com.study.common.entity.ResultInfo;
import com.study.common.entity.User;
import com.study.common.enumerate.ExceptionInfo;
import com.study.common.utils.I18nUtils;
import com.study.common.utils.ResultUtils;
import com.study.login.service.LoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.http.HttpSession;

/**
 * @Create by ldr.
 * @create 2018-08-28 14:25
 * @Description：登陆控制器
 */
@Controller
@RequestMapping(value = "/demo")
public class LoginController {

    @Autowired
    @Qualifier(value = "loginService")
    private LoginService loginService;

    /**
     * 进入登陆页面
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "/login";
    }


    /**
     * description: 登陆认证
     * @params 1.session 2.输入的用户名和密码
     * @return 1 成功 用户信息
     */
    @RequestMapping(value = "/subLogin",method = RequestMethod.POST)
    @ResponseBody
    public ResultInfo<User> subLogin(Locale locale,HttpSession session, @RequestBody User user){
        /**
         * 权限认证交给 com.study.common.security.MyPermission 处理
         */
        Subject subject = SecurityUtils.getSubject();
        //测试当前用户是否已经被认证(即是否已经登录)
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),
                    user.getPassWord());
            //token.setRememberMe(true);//记录用户
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            return new ResultUtils().fail(I18nUtils.getrb(locale,"loginResult"));
        }

        // 通过输入的用户名查询用户信息
        User userInfo = loginService.getUserByUserName(user.getUserName());
        //把用户信息存入session
        session.setAttribute("userInfo",userInfo);
        return new ResultUtils().success(userInfo,"");

    }

    @RequiresPermissions("all")
    @RequestMapping(value = "a")
    @ResponseBody
    public ResultInfo a(Locale locale, HttpSession session) throws NoSuchFieldException, IllegalAccessException {
        ResultUtils resultUtils = new ResultUtils();
        session.setAttribute("locale",locale);
        ResourceBundle rb = ResourceBundle.getBundle("i18n.messages", locale);
        return resultUtils.success(null,"") ;
    }
    @RequestMapping(value = "b")
    public String b(){
        return "index";
    }
    @RequestMapping(value = "c")
    public String c(){
        return "main/main";
    }
    /**
     * description: 退出 移除session
     * @params session 存的用户信息
     * @return 1 成功
     */
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public String logout(HttpSession session){
        session.removeAttribute("userInfo");
        return "/login";
    }
}
